﻿var zombies = zombies || {};

zombies.adminmaster = function () {
    var self = this;
    self.isOpen = ko.observable(false);


    self.Views = ko.observable();
    self.Likes = ko.observable();
    self.Rate = ko.observable();
    //login
    //self.UserName = ko.observable();
    //self.Password = ko.observable();
    //self.doLogin = function (element) {
    //    var account = { UserName: self.UserName, Password: self.Password };
    //    if ($(element).valid()) {
    //        $.ajax({
    //            type: "POST",
    //            url: "/api/Account/Login",
    //            data: ko.toJSON(account), //Convert the Observable Data into JSON
    //            contentType: "application/json",
    //            dataType: 'json'
    //        }).done(function () {
    //            var returnUrl = $.getUrlParams("ReturnUrl");
    //            $("#container-register").toggleClass("register-show");
    //            if ($("#container-register").hasClass("register-show")) {
    //                $("#container-register").animate({
    //                    top: "+=100%"
    //                });
    //            } else {
    //                $("#container-register").animate({
    //                    top: "-=100%"
    //                });
    //            }
    //            toastr.success('Đăng Nhập Thành Công');

    //            if (!returnUrl) {
    //                window.location.reload();
    //            } else {
    //                returnUrl = returnUrl.replace("%2f", "/");
    //                window.location.replace('http://' + window.location.host + returnUrl);
    //            }

    //        }).fail(function (request) {
    //            var msg = request.responseText.substring(12);
    //            msg = msg.substring(0, msg.length - 2);
    //            toastr.error(msg);
    //        });
    //    }
    //};

    self.openSidebar = function () {
        if (!self.isOpen()) {
            $('#page-container').addClass('sidebar-visible-xs');
            self.isOpen(true);
        } else {
            $('#page-container').removeClass('sidebar-visible-xs');
            self.isOpen(false);
        }
    };

    self.doLogout = function () {
        $.ajax({
            type: "POST",
            url: "/api/Account/Logout",
            contentType: "application/json",
            dataType: 'json'
        }).done(function () {
            toastr.info('Bạn đã thoát khỏi hệ thống');
            window.location.replace('http://' + window.location.host);
        }).fail(function (request) {
            var msg = request.responseText.substring(12);
            msg = msg.substring(0, msg.length - 2);
            toastr.error(msg);
        });
    }
};

var loadHeader = function () {
    $.ajax({
        url: '/api/Statistic/GetViewLikeRate',
        type: 'GET',
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    }).done(function (item) {
        adminMasterModel.Views(item.Views === null? 0: item.Views);
        adminMasterModel.Likes(item.Likes === null? 0: item.Likes);
        adminMasterModel.Rate(item.RateCount === null? 0: item.RateCount);
    }).error(function () {
        toastr.error('error');
    })
}

var adminMasterModel;
$(document).ready(function () {
    moment().format();
    moment().lang('vn');
    toastr.options = {
        "closeButton": false,
        "debug": false,
        "positionClass": "toast-bottom-right",
        "onclick": null,
        "showDuration": "300",
        "hideDuration": "1000",
        "timeOut": "5000",
        "extendedTimeOut": "1000",
        "showEasing": "swing",
        "hideEasing": "linear",
        "showMethod": "fadeIn",
        "hideMethod": "fadeOut"
    }
    adminMasterModel = new zombies.adminmaster();

    loadHeader();

    ko.applyBindings(adminMasterModel, document.getElementById("sidebar"));
    ko.applyBindings(adminMasterModel, document.getElementById("admin_cover"));
});